package cn.itcast.dao;


import cn.itcast.pojo.User;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.web.client.RestTemplate;

import java.util.List;

//@Repository
public class UserDao {
    private static final Logger logger = LoggerFactory.getLogger(UserDao.class);
    @Autowired
    private RestTemplate restTemplate;

    @HystrixCommand(fallbackMethod = "queryUserByIdFallbackMethod")
    public User queryUserById(Long id) {
        long strat = System.currentTimeMillis();
        String url = "http://localhost:8081/user/" + id;
        User user = restTemplate.getForObject(url, User.class);
        long end = System.currentTimeMillis();
        long time = end - strat;
        System.out.println("访问时间time = " + time);
        return user;
    }

    public User queryUserByIdFallbackMethod(Long id) {
        User user = new User();
        user.setId(id);
        user.setName("用户信息异常");
        return user;
    }
}
